<?php
namespace app\api\controller;

use app\admin\model\Couponuse;
use app\common\model\User;
use app\common\model\Usermemberhistory;
use app\api\service\Token as TokenService;
use app\common\BaseHelper;
use app\admin\model\Orderoffline;

class OfflineOrder extends BaseController
{
    public function createOrder() {
        $param = self::getHttpParam();
        $price = $param->price;  //消费总金额
        $solidFee = empty($param->solidFee) ? 0 : $param->solidFee;  //不参与优惠金额        
        $oldPrice = $price - $solidFee;  //可优惠的金额
        if(empty($price)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'price不能为空');
        }
        $userId = TokenService::getCurrentUid();
        $data = [
            'id' => BaseHelper::getUUID(),
            'orderNO' => \app\api\service\Order::makeOrderNo(),
            'price' => $price*100,
            'solidFee' => $solidFee*100,
            'createUser' => $userId,
            'tenantId' => TokenService::getTenantid(),
            'storeId' => $param->storeId,
            'created' => time()
        ];
        $discountprice = 0;
        if($param->couponId){  //选择优惠券
            $Couponuse = Couponuse::where(['couponId'=>$param->couponId,'userId'=>$userId,'status'=>0])->find();
            if($Couponuse){
                $Coupondetails = json_decode($Couponuse->couponDetail,true);
                if($oldPrice >= $Coupondetails['value1']/100){                   
                    if($oldPrice <= $Coupondetails['value2']/100) {  //优惠金额小于代金券面值                        
                        $discountprice = $oldPrice;
                        $oldPrice = 0;
                    } else {
                        $oldPrice -= $Coupondetails['value2']/100;
                        $discountprice = $Coupondetails['value2']/100;
                    }
                }
                $data['couponId'] = $param->couponId;
            }else{
                throw new ParameterException([
                    'msg' => '优惠券信息异常'
                ]);
            }
        }

        if($param->memberId){  //选择会员折扣
            $user = User::where(['id'=>$userId,'typeId'=>$param->memberId])->where('memberendtime'>time())->find();
            if($user){
                $Usermemberhistory = Usermemberhistory::where(['userId'=>$user->id,'membertype_id'=>$param->memberId])->find();
                if($Usermemberhistory){  //会员价
                    if($Usermemberhistory->isMemBerShop == 1){
                        $oldPrice = $oldPrice * ($Usermemberhistory->memberDiscounts*10/100);
                        $discountprice = $price - $solidFee - $oldPrice;
                    }
                }
            }
            $data['memberId'] = $param->memberId;
        }
        $data['total'] = ($oldPrice + $solidFee) == 0 ? 1: ($oldPrice + $solidFee) * 100;  //若支付总金额为0，则支付1分钱
        $data['saleMoney'] = $discountprice * 100;
        $save = Orderoffline::insert($data);
        if($save) {
            return show(config('status.SUCCESS_STATUS'), '创建订单成功', $data);
        }
        return show(config('status.ERROR_STATUS'), self::NOT_DATA, '创建失败');
    }


}